﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mvvm;
using CodeIn.Modules;
using CodeIn.Core;
using System.Windows.Data;

namespace CodeIn.Ui.StatusBar {
	public class StatusBarViewModel : BaseViewModel {
		private string status;
		public string Status {
			get { return status; }
			set { status = value; FirePropertyChanged("Status"); }
		}

		private double currentopacity;
		public double CurrentOpacity {
			get { return currentopacity; }
			set { currentopacity = value; FirePropertyChanged("CurrentOpacity"); }
		}

		private string latestModuleName;
		public string LatestModuleName {
			get { return latestModuleName; }
			set { latestModuleName = value; FirePropertyChanged("LatestModuleName"); }
		}

		private int currentProgress;
		public int CurrentProgress {
			get { return currentProgress; }
			set { currentProgress = value; FirePropertyChanged("CurrentProgress"); }
		}

		private string runningModuleDescription;
		public string RunningModuleDescription {
			get { return runningModuleDescription; }
			set { runningModuleDescription = value; FirePropertyChanged("RunningModuleDescription"); }
		}

		public IList<string> Themes { get; private set; }

		public StatusBarViewModel() {
			CurrentOpacity = 0;
			Status = "Ready";
			InitializeModuleManagerCallBacks();

			Themes = new List<string>();
			foreach (string theme in ResourceManager.Instance.Themes.Keys)
				Themes.Add(theme);

			var opt = OptionManager.Instance.GetOptionFromName("System.Ui.CurrentTheme");
			var view = CollectionViewSource.GetDefaultView(Themes);
			view.MoveCurrentTo(opt.Value);
			view.CurrentChanged += (s, e) => {
				opt.Value = view.CurrentItem.ToString();
			};
			opt.ValueChanged += (s, e) => {
				view.MoveCurrentTo(e.NewValue);
			};
		}

		private void InitializeModuleManagerCallBacks() {
			ModuleManager.Instance.ModuleExecutionStarted += (s, e) => {
				UiHelper.Synchronize(() => {
					HandleModuleStarted(); 
				});
			};

			ModuleManager.Instance.ModuleExecutionProgressed += (s, e) => {
				UiHelper.Synchronize(() => {
					if (e.Module == ModuleManager.Instance.LatestRunningModule)
						HandleModuleProgressed(e.Progress);
				});
			};

			ModuleManager.Instance.ModuleExecutionEnded += (s, e) => {
				UiHelper.Synchronize(() => { 
					HandleModuleEnded(); 
				});
			};
		}

		private void HandleModuleStarted() {
			int runningModuleCount = ModuleManager.Instance.RunningModules.Count;
			var latestModule = ModuleManager.Instance.LatestRunningModule;

			LatestModuleName = latestModule == null ? "null" : latestModule.Infos.Name;

			if (runningModuleCount == 1) {
				RunningModuleDescription = "1 module is running, ";
			} else if (runningModuleCount > 1) {
				RunningModuleDescription = runningModuleCount + " modules are running, latest is ";
			}

			CurrentProgress = 0;
			CurrentOpacity = 1;
			Status = "Busy...";
		}

		private void HandleModuleProgressed(int progress) {
			CurrentProgress = progress;
		}

		private void HandleModuleEnded() {
			int runningModuleCount = ModuleManager.Instance.RunningModules.Count;
			var latestModule = ModuleManager.Instance.LatestRunningModule;

			LatestModuleName = latestModule == null ? "null" : latestModule.Infos.Name;

			if (runningModuleCount == 0) {
				CurrentOpacity = 0;
				Status = "Ready";
			} else if (runningModuleCount == 1) {
				RunningModuleDescription = "1 module is running, ";
			} else {
				RunningModuleDescription = runningModuleCount + " modules are running, latest is ";
			}
		}
	}
}
